package cn.hhy.org.controller;

import cn.hhy.org.domain.Shop;
import cn.hhy.org.query.ShopQuery;
import cn.hhy.org.service.IShopService;
import cn.hhy.utils.AjaxResult;
import cn.hhy.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;

    @GetMapping
    public List<Shop> loadAll(){
        return shopService.loadAll();

    }
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null) {
                shopService.update(shop);
            } else {
                shopService.save(shop);
            }

            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败！" + e.getMessage());
        }





    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {

        try {
            shopService.delete(id);


            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败！" + e.getMessage());
        }

    }
    @GetMapping("/{id}")
    public  Shop loadById(@PathVariable("id") Long id){
        return shopService.loadById(id);

    }
    @PatchMapping
    public AjaxResult PatchDelete(@RequestBody Long[] ids) {

        try {
            shopService.PatchDelete(ids);

            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败！" + e.getMessage());
        }

    }
    @PostMapping
    public QueryResult<Shop> shopLoadAll(@RequestBody ShopQuery shopQuery){

        return shopService.shopLoadAll(shopQuery);



    }


}
